package cn.edu.scau.cmi.chenmiaopeng.compositePattern.transparentPattern;
import java.util.ArrayList;

import cn.edu.scau.cmi.chenmiaopeng.compositePattern.safePattern.Organization_S;
public class Team_T extends Organization_T{
	private String name;
	private int recursionNum;
	private int organizationIndividalCount = 0;  //集体个人计数
	private static boolean isPersonal;  //表示要操作的是个人还是团队
	private ArrayList<Organization_T> organizationMembers;
	private static Team_T orginalOrg;  //祖结点
	private Team_T thisTeam;  //表示要进行操作的父节点  static变量 会让所有的team共有一个thisTeam,所以无限循环，跳不出来
	private static boolean isCreateSuccess;  //表示父节点创建子节点操作是否成功
	private boolean isOrganizationExist = false;  //表示要操作的节点是否存在
	private boolean removeOrDelete;               //该变量表示是移除还是删除，true:移除；false:删除
	public Team_T() {
		organizationMembers = new ArrayList<Organization_T>();
	}
	public static void setOriginalOrg(Team_T org) {
		orginalOrg = org;
	}
	public void setName(String orgName) {
		name = orgName;
	}
	public static void setIsPerson(boolean isPerson) {
		isPersonal = isPerson;
	}
	public String getName() {
		return name;
	}
	public String getOrganization(int instanceIndex) {
		if(instanceIndex<organizationMembers.size()) {
			return organizationMembers.get(instanceIndex).getName();
		}
		else return null;
	}
	public ArrayList<Organization_T> getOrganizationArrayList() {
		return organizationMembers;
	}
	public static boolean getIsCreateSuccess() {
		return isCreateSuccess;
	}
	public int getOrganizationIndividalCount() {  //返回该集体的个人数
		return organizationIndividalCount;
	}
	public void setRemoveOrDelete(boolean remove_delete) {
		removeOrDelete = remove_delete;
	}
	public boolean getRemoveOrDelete() {
		return removeOrDelete;
	}
//	public void addPersonal(Personal person) {
//		organizationMembers.add(person);
//	}
//	public void addTeam(Team team) {
//		organizationMembers.add(team);
//	}
	public String add(String instanceName) {
		isOrganizationExist = false;
		isOrganizationExists(instanceName,orginalOrg);
 		if(isOrganizationExist) {
 			isCreateSuccess = false;
			return "成员已存在，请查证！";
		}
		else {
			if(isPersonal) {
			    Personal_T newMember = new Personal_T(instanceName);
			    organizationMembers.add(newMember);
				organizationIndividalCount++;  //加个人
				isCreateSuccess = true;
				return "加入个人成功！当前该集体个人个数为:"+organizationIndividalCount;
			}
			else {
				Team_T newMember = new Team_T();
				newMember.setName(instanceName);
				organizationMembers.add(newMember);
//			    organizationIndividalCount += organizationIndividalCount;
				isCreateSuccess = true;
				return "加入集体成功！当前该集体个人个数为:"+organizationIndividalCount;
			}
		}
//		
	}


	public String remove(String instanceName) {
		isOrganizationExist = false;  //每次查询前将变量isOrganizationExist置为false
		isOrganizationExists(instanceName,orginalOrg);
		if(!isOrganizationExist) {
			return "所查成员不存在！";
		}
		else {
			if(isPersonal) {
				for(int i=0;i<organizationMembers.size();i++) {
					if(organizationMembers.get(i).getName().equals(instanceName)) {
						Organization_T removeOrganzation = organizationMembers.get(i);
						organizationMembers.remove(i);
						organizationIndividalCount--;
						if(!removeOrDelete)
						orginalOrg.getOrganizationArrayList().add(removeOrganzation);
					}
				}
				return "去除个人成功！"+"当前该集体个人个数为:"+organizationIndividalCount;
			}
			else {
//				Team_T removeOrg = new Team_T();
//				removeOrg.setName(instanceName);
				for(int i=0;i<organizationMembers.size();i++) {
					if(organizationMembers.get(i).getName().equals(instanceName)) {
						Team_T removeOrg = (Team_T)organizationMembers.get(i);
						organizationMembers.remove(i);
						organizationIndividalCount-=removeOrg.getOrganizationIndividalCount();
						if(!removeOrDelete)
						orginalOrg.getOrganizationArrayList().add(removeOrg);
						break;
					}
				}
				return "去除集体成功！"+"当前该集体个人个数为:"+organizationIndividalCount;
			}
		}
			
		}
	public int getOrgMemCount() {
		return organizationIndividalCount;
	}
	public void setRecursionNum(int recursionNum) {
		this.recursionNum = recursionNum;
	}
	public String greet() {  //recursionNum: 递归层次数：外部调用，初值为0
		StringBuffer sb = new StringBuffer();
		String preBlankString = returnPreBlanks(recursionNum);
		//若当前节点为根节点，添加全局的输出
		if(this==orginalOrg) {
			sb.append("All the organizations(transparent mode) are listed below:\n");
	    }
		else {
			sb.append(preBlankString+"Hello, we are "+name+",we have "+organizationIndividalCount+" individuals."+"\n"+preBlankString+"  Our members are listed below:\n");
		}
		for(int i=0;i<organizationMembers.size();i++) {
			Organization_T thisTestInstance = organizationMembers.get(i);
			if(thisTestInstance instanceof Team_T) {
				((Team_T) thisTestInstance).setRecursionNum(recursionNum+1);
				sb.append(thisTestInstance.greet());
			}
			else sb.append(preBlankString+"  -"+"Member "+(i+1)+": "+thisTestInstance.greet()+"\n");  //个人前加"-",在每个团体下的个人都要往后退两格，输出个人信息后换行
		}
		return new String(sb);
	}
	private String returnPreBlanks(int recursionNum) {
		StringBuffer blankSb = new StringBuffer();
		for(int j=0;j<recursionNum;j++) {
			blankSb.append("  ");
		}
		return new String(blankSb);
	}

	  private void isOrganizationExists(String orgName,Team_T org) {  
		  //递归地查询是否存在org,注意：organizationMembers是相对于函数变量的organizationMembers,而不是最开始的Team的成员变量organizationMembers
		  for(int i=0;i<org.organizationMembers.size();i++) {
			  if(org.organizationMembers.get(i) instanceof Team_T) {
				  if(!isPersonal&&org.organizationMembers.get(i).getName().equals(orgName)) isOrganizationExist = true;
				  System.out.println("当前的team是："+org.organizationMembers.get(i).getName());
				  thisTeam = (Team_T)org.organizationMembers.get(i);
				   isOrganizationExists(orgName,thisTeam);  
				   //不能在这里return isOrganizationExists(), 而应该用下层递归函数给该层变量赋值，最后的输出结果由该层变量决定
				  //因为该分支没有，不代表同层的其他分支没有
			  }
			  else {
				  if(org.organizationMembers.get(i).getName().equals(orgName)) {
					  if(isPersonal && (org.organizationMembers.get(i) instanceof Personal_T)) isOrganizationExist =  true;
				  }
			  }
		  }
	  }
	  public boolean equals(Object other) {
		  if(other==null) return false;
		  else if(other instanceof Team_T) {
			  return false;
		  }
		  else {
			  Team_T otherTeam = (Team_T)other;
			  if(name.equals(otherTeam.getName())) {
				  return true;
			  }
			  else return false;
		  }
	  }
}

